package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.AppVariable;
import com.example.demo.common.PasswordUtils;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.UserInfo;
import com.example.demo.entity.vo.UserInfoVO;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author vivi
 * @description
 * @date 2024/6/1
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/reg")
    public AjaxResult reg(UserInfo userInfo) {
        // 非空校验 和 参数有效性校验
        if (userInfo == null || ! StringUtils.hasLength(userInfo.getUsername()) ||
                !StringUtils.hasLength(userInfo.getPassword())) {
            return AjaxResult.fail(-1, "非法参数！");
        }
        // 密码加密
        userInfo.setPassword(PasswordUtils.encrypt(userInfo.getPassword()));
        return AjaxResult.success(userService.reg(userInfo));
    }

    @RequestMapping("/login")
    public AjaxResult login(HttpServletRequest request, String username, String password) {
        // 非空校验
        if (!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            return AjaxResult.fail(-1,"非法请求！");
        }
        // 查询数据库
        UserInfo userInfo = userService.getUserByName(username);
        if (userInfo != null && userInfo.getId() > 0) {
            // 有效的用户名
            // 判断两个密码是否相同
            if (PasswordUtils.check(password, userInfo.getPassword())) {
                // 登陆成功
                // 将用户存储到 session
                HttpSession session = request.getSession(); // 参数默认为true
                session.setAttribute(AppVariable.USER_SESSION_KET, userInfo);
                // 返回前端之前， 隐藏敏感信息
                userInfo.setPassword("");

                return AjaxResult.success(userInfo);
            }
        }
        return AjaxResult.success(0,null);
    }

    @RequestMapping("/showinfo")
    public AjaxResult showInfo(HttpServletRequest request) {
        UserInfoVO userInfoVo = new UserInfoVO();
        // 1 得到当前登录用户
        UserInfo userInfo = UserSessionUtils.getSessionUser(request);
        if (userInfo == null) {
            return AjaxResult.fail(-1, "非法请求");
        }
        // spring 提供的 深克隆 方法
        BeanUtils.copyProperties(userInfo, userInfoVo);
        // 2 得到用户发表文章的总数
        userInfoVo.setArtCount(articleService.getArtCountByUid(userInfo.getId()));
        return AjaxResult.success(userInfoVo);
    }

    // 注销操作
    @RequestMapping("/logout")
    public AjaxResult logout(HttpSession session) {
        session.removeAttribute(AppVariable.USER_SESSION_KET);
        return AjaxResult.success(1);
    }

    @RequestMapping("/getuserbyid")
    public AjaxResult getUserById(Integer id) {
        if (id == null || id <= 0) {
            // 无效参数
            return AjaxResult.fail(-1,"非法参数");
        }
        UserInfo userInfo = userService.getUserById(id);
        if (userInfo == null || userInfo.getId() <= 0) {
            return AjaxResult.fail(-1, "非法参数");
        }
        // 去除敏感信息
        userInfo.setPassword("");

        UserInfoVO userInfoVO = new UserInfoVO();
        // 深拷贝--将 userInfo 中的所有内容深拷贝到 userInfoVO
        BeanUtils.copyProperties(userInfo, userInfoVO);
        // 查询当前用户发表的文章数
        userInfoVO.setArtCount(articleService.getArtCountByUid(id));
        //                                                     此处的id为传进来的参数
        return AjaxResult.success(userInfoVO);
    }

}



























